<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>Tiny Mock 控制台-登录</title>
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta http-equiv="Access-Control-Allow-Origin" content="*">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <meta name="mobile-web-app-capable" content="yes">
    <meta name="format-detection" content="telephone=no">
    <meta name="Keywords" content="TINY平台,TINYMOCK,mock,Mock,在线mock服务,前后端分离,在线mock,在线接口Mock平台,免费mock服务"/>
    <meta name="Description" content="Tiny Mock 是一个可视化, 易操作的能快速生成模拟数据的Mock服务平台"/>
    <link rel="icon" href="/images/favicon.ico">
    <link rel="stylesheet" href="/lib/layui/css/layui.css?v=2.9.25" media="all">
    <!--[if lt IE 9]>
    <script src="https://cdn.staticfile.org/html5shiv/r29/html5.min.js"></script>
    <script src="https://cdn.staticfile.org/respond.js/1.4.2/respond.min.js"></script>
    <![endif]-->
    <style>
        html, body {
            height: 100%;
            margin: 0;
            padding: 0;
        }

        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
            outline: none !important;
        }

        .login-container {
            width: 100%;
            height: 100%;
            background: url(/images/login/login-bg.png) no-repeat center;
            background-size: cover;
            display: flex;
            align-items: center;
            justify-content: center;
        }

        .login-container .box-item {
            width: 444px;
            height: 600px;
        }

        .login-container .box-item.desc {
            background: #ffffff;
            border-radius: 12px 0px 0px 12px;
            box-shadow: 0px 16px 73px 8px rgba(203, 203, 203, 0.2);
            padding: 23px 25px;
        }

        .login-container .box-item.login {
            background: #ffffff;
            border-radius: 0px 12px 12px 0px;
            padding: 34px 42px;
            position: relative;
        }

        .login-container .box-item .login-qr {
            position: absolute;
            top: 0;
            right: 0;
            width: 66px;
            height: 66px;
        }

        .login-container .box-item .welcome {
            background: url(/images/login/left-bg1.png) no-repeat center;
            background-size: cover;
            height: 100%;
            border-radius: 8px;
            box-shadow: 0px 6px 20px 0px rgba(33, 47, 117, 0.1);
            padding-top: 35px;
        }

        .login-container .box-item .welcome p {
            color: #333333;
            line-height: 25px;
            letter-spacing: 0.26px;
            text-align: center;
            font-weight: 700;
            font-size: 26px;
        }

        .login-container .box-item .welcome .sub-welcome {
            color: #333333;
            line-height: 25px;
            letter-spacing: 0.26px;
            font-weight: 700;
            font-size: 20px;
            margin-top: 10px;
        }

        .login-container .box-item .login-title {
            font-size: 30px;
            font-weight: 700;
            text-align: center;
            color: #1e1e1e;
            margin-bottom: 35px;
            margin-top: 35px;
        }

        .login-container .more {
            margin-top: 30px;
        }

        .login-container .more .title-box {
            display: flex;
            align-items: center;
            justify-content: center;
        }

        .login-container .more .title-box > p {
            margin-bottom: 0;
        }

        .login-container .more .line {
            width: 114px;
            height: 1px;
            background: #e6e6e6;
        }

        .login-container .more .title {
            font-size: 14px;
            font-weight: 500;
            color: #a1aebe;
            margin: 0 19px;
        }

        .login-container .more .login-type {
            padding: 0 5px;
            margin-top: 25px;
            display: flex;
            align-items: center;
            justify-content: space-between;
        }

        .login-container .more .login-type > img {
            width: 30px;
            height: 30px;
        }

        .captcha-img img {
            border: 1px solid #e6e6e6;
            height: 36px;
            width: 100%;
            cursor: pointer;
        }
    </style>
</head>
<body>

<div class="login-container">
    <div class="box-item desc">
        <div class="welcome">
            <p>欢迎使用 TINY-MOCK</p>
            <p class="sub-welcome">「简洁、好用、安全」的在线接口Mock平台</p>
        </div>
    </div>
    <div class="box-item login">
        <img class="login-qr" src="/images/login/login-qr.png" alt="二维码"/>
        <div class="login-title">后台登录</div>

        <div class="login-form">
            <form class="layui-form" action="">
                <div class="layui-form-item">
                    <div class="layui-input-wrap">
                        <div class="layui-input-prefix">
                            <i class="layui-icon layui-icon-username"></i>
                        </div>
                        <input type="text" name="username" value="" lay-verify="required|username" lay-vertype="tips" placeholder="用户名" lay-reqtext="请填写用户名" autocomplete="off" class="layui-input" lay-affix="clear">
                    </div>
                </div>
                <div class="layui-form-item">
                    <div class="layui-input-wrap">
                        <div class="layui-input-prefix">
                            <i class="layui-icon layui-icon-password"></i>
                        </div>
                        <input type="password" name="password" value="" lay-verify="required|password" lay-vertype="tips" placeholder="密  码" lay-reqtext="请填写密码" autocomplete="off" class="layui-input" lay-affix="eye">
                    </div>
                </div>
                <div class="layui-form-item">
                    <div class="layui-row">
                        <div class="layui-col-xs7">
                            <div class="layui-input-wrap">
                                <div class="layui-input-prefix">
                                    <i class="layui-icon layui-icon-vercode"></i>
                                </div>
                                <input type="text" name="captcha" value="" lay-verify="required|captcha" lay-vertype="tips" placeholder="验证码" lay-reqtext="请填写验证码" autocomplete="off" class="layui-input" lay-affix="clear">
                            </div>
                        </div>
                        <div class="layui-col-xs5">
                            <div style="margin-left: 10px;" class="captcha-img">
                                <img id="captcha" lay-on="getCaptcha"  alt="验证码">
                            </div>
                        </div>
                    </div>
                </div>
                <div class="layui-form-item">
                    <a class="layuimini-link" href="/page/register.html">注册账号</a>
                    <a class="layuimini-link" style="float: right;" href="/page/forget.html">忘记密码？</a>
                </div>
                <div class="layui-form-item">
                    <button class="layui-btn layui-btn-fluid" lay-submit lay-filter="login" id="login">登 录</button>
                </div>
            </form>
        </div>

        <div class="more">
            <div class="title-box">
                <p class="line"></p>
                <p class="title">其他方式登录</p>
                <p class="line"></p>
            </div>
            <div class="login-type">
                <img src="/images/login/wechat-icon.png" alt="wechat"/>
                <img src="/images/login/ali-icon.png" alt="ali"/>
                <img src="/images/login/douyin-icon.png" alt="douyin"/>
                <img src="/images/login/qq-icon.png" alt="qq"/>
                <img src="/images/login/weibo-icon.png" alt="weibo"/>
                <img src="/images/login/feishu-icon.png" alt="feishu"/>
                <img src="/images/login/google-icon.png" alt="google"/>
            </div>
        </div>
    </div>
</div>

<script src="/lib/cipher/sm3.js?v=2.0.0" charset="utf-8"></script>
<script src="/lib/cipher/sm2.js?v=2.0.0" charset="utf-8"></script>
<script src="/lib/layui/layui.js?v=2.9.25" charset="utf-8"></script>
<script src="/js/lay-config.js?v=2.9.25" charset="utf-8"></script>
<script src="/js/public.js?v=2.9.25" charset="utf-8"></script>
<script>
    var verifyKey;
    var publicKey;

    layui.use(['form', 'layer', 'util', 'miniAjax'], function () {
        var $ = layui.jquery,
            form = layui.form,
            util = layui.util,
            miniAjax = layui.miniAjax,
            layer = layui.layer;

        // 登录过期的时候，跳出iframe框架
        if (top.location != self.location) {
            top.location = self.location;
        }

        util.on('lay-on', {
            getCaptcha: function () {
                getCaptcha();
            },
        });

        getCaptcha();

        form.verify({
            username: function (value, elem) {
                if (value.length > 32) {
                    return ('用户名不能超过32个字符！');
                }
            },
            password: function (value, elem) {
                if (value.length > 20) {
                    return ('用户密码不能超过20个字符！');
                } else if (value.length < 8) {
                    return '用户密码不能少于8个字符！';
                }
            },
            captcha: function (value, elem) {
                if (value.length !== 5) {
                    return ('验证码必须是5个字符！');
                }
            }
        });

        // 进行登录操作
        form.on('submit(login)', function (data) {
            data = data.field;
            // 先对密码做哈希运算
            let hashData = sm3(data.password + CommonConfig.passwordSalt);
            // 然后再做sm2公钥加密
            let encryptData = sm2.doEncrypt(hashData, publicKey);
            // 加密结果
            data.password = encryptData;
            data.uuid = verifyKey;

            startLoading();
            miniAjax.postJSON({
                url: "/auth/login",
                data: JSON.stringify(data),
                success: function (res) {
                    stopLoading();
                    if (res.code == 0) {
                        sessionStorage.setItem('token', res.data);
                        sessionStorage.setItem('username', data.username);
                        layer.msg('登录成功，欢迎回来!', {
                            time: 1000
                        }, function () {
                            window.location = '/';
                        });
                    } else {
                        layer.msg(res.msg, {time: 1000});
                        getCaptcha();
                    }
                },
                error: function (res) {
                    stopLoading();
                    layer.msg(res, {time: 1000, icon: 2});
                }
            });
            return false;
        });

       // 为所有登录方式图片注册点击事件
        $('.login-type').on('click', 'img[alt]', function () {
            var type = $(this).attr('alt');
            switch (type) {
                case 'wechat':
                    layer.msg('你点击了微信图标');
                    break;
                case 'ali':
                    layer.msg('你点击了阿里图标');
                    break;
                case 'douyin':
                    layer.msg('你点击了抖音图标');
                    break;
                case 'qq':
                    layer.msg('你点击了QQ图标');
                    break;
                case 'weibo':
                    layer.msg('你点击了微博图标');
                    break;
                case 'feishu':
                    layer.msg('你点击了飞书图标');
                    break;
                case 'google':
                    layer.msg('你点击了谷歌图标');
                    break;
                default:
                    break;
            }
        });

        // 获取图形验证码
        function getCaptcha() {
            miniAjax.get({
                url: "/auth/getCode",
                success: function (res) {
                    $("#captcha").prop("src", "data:image/gif;base64," + res.data.img);
                    verifyKey = res.data.uuid;
                    publicKey = res.data.publicKey;
                },
                error: function (error) {
                    layer.msg(error, {time: 1000, icon: 2});
                }
            });
        }
    });


    function startLoading() {
        var loginBtn = document.querySelector('#login');
        loginBtn.classList.add('layui-disabled');
        loginBtn.setAttribute('disabled', true);
        loginBtn.innerHTML = '<span>登录中</span><i class="layui-icon layui-icon-loading layui-anim layui-anim-rotate layui-anim-loop"></i>';
    }

    function stopLoading() {
        var loginBtn = document.querySelector('#login');
        loginBtn.classList.remove('layui-disabled');
        loginBtn.removeAttribute('disabled');
        loginBtn.innerHTML = '<span>登 录</span>';
    }
</script>
</body>
</html>